using System;
using System.Collections.Generic;
using System.Text;
using Lizk.SimpleHUD;
using SharpMedia.Controls;
using System.Data;
using MediaData;

namespace SharpMedia.Pages
{
    public class ScraperSearchPage : MediaPage
    {
        HUDTextBox textBox;
        HUDTextBoxInput input;
        HUDImage box, icon, button;
        HUDLabel lblTitle;
        HUDContainerSimple boxContainer;
        MediaDataSet.TitlesRow title;



        public ScraperSearchPage(HUDPage parent, MediaDataSet.TitlesRow title)
            : base(parent)
        {
            this.parent = parent;
            this.title = title;
            InitializeComponents();
        }
        private void InitializeComponents()
        {
            HUDImage background = new HUDImage("white.png");
            background.Size = Size;
            background.Color = ColorS.Black;
            background.ConstrainProportions = false;
            background.Alpha = 170;
            Add(background);


            boxContainer = new HUDContainerSimple();
            boxContainer.Size = new SizeS(500, 250);
            Add(boxContainer);



            box = new HUDImage("box.png");
            box.ConstrainProportions = false;
            box.Size = boxContainer.Size;
            boxContainer.Add(box);

            icon = new HUDImage("web.png");
            icon.Size = new SizeS(64, 64);
            icon.Location = new PointS(20, 20);
            boxContainer.Add(icon);

            lblTitle = new HUDLabel("Scrape " + title["name"], "tahoma30b");
            lblTitle.Size = new SizeS(boxContainer.Size.Width - icon.Bounds.Right - 10, 64);
            lblTitle.Location = new PointS(icon.Bounds.Right + 5, icon.Bounds.Top);
            lblTitle.Alignment = Alignment.VCenter | Alignment.Left;
            boxContainer.Add(lblTitle);

            textBox = new HUDTextBox("white.png", "tahoma30b");
            textBox.Size = new SizeS(300, 32);
            textBox.Location = new PointS(50, 150);
            textBox.Label.Text = title["name"].ToString();
            boxContainer.Add(textBox);

            button = new HUDImage("webbutton.png");
            button.Size = new SizeS(70, 70);
            button.Location = new PointS(textBox.Bounds.Right + 20, textBox.Bounds.Top + textBox.Bounds.Height / 2f - 32);
            boxContainer.Add(button);


            boxContainer.Location = new PointS(Bounds.Width / 2f - boxContainer.Size.Width / 2f, Bounds.Height / 2f - boxContainer.Size.Height / 2f);

            input = new HUDTextBoxInput(textBox);
            boxContainer.Add(input);
            input.UpdatePosition(textBox, false);


        }

        public override void ResizeMove(RectangleS oldRect, RectangleS newRect)
        {
            base.ResizeMove(oldRect, newRect);
            if (boxContainer != null)
            {
                //textBox.Location = new PointS(Bounds.Width / 2f - textBox.Size.Width / 2f, Bounds.Height / 2f - textBox.Size.Height / 2f);
                boxContainer.Location = new PointS(Bounds.Width / 2f - boxContainer.Size.Width / 2f, Bounds.Height / 2f - boxContainer.Size.Height / 2f);
            }
        }

        public override void Show()
        {
            Fade(255, 6f);
            base.Show();
        }

        public override void Activate()
        {
            Fade(255, 6f);
            base.Activate();
        }

        public override void Close()
        {
            Fade(0, 6f);
            base.Close();
        }

        public override void Command(Lizk.SimpleHUD.HUDCommands command, object tag)
        {
            switch (command)
            {
                case Lizk.SimpleHUD.HUDCommands.Up:
                    input.SelectedIndex--;
                    break;
                case Lizk.SimpleHUD.HUDCommands.Down:
                    input.SelectedIndex++;
                    break;
                case Lizk.SimpleHUD.HUDCommands.Back:
                case Lizk.SimpleHUD.HUDCommands.Left:
                    if (textBox.Label.Text.Length == 0)
                    {
                        SharpMedia.Back();
                        break;
                    }
                    textBox.Label.Text = textBox.Label.Text.Remove(textBox.Label.Text.Length - 1);
                    textBox.GetTextWidth();
                    input.UpdatePosition(textBox, true);
                    break;
                case Lizk.SimpleHUD.HUDCommands.Right:
                    textBox.Label.Text += ' ';
                    textBox.GetTextWidth();
                    input.UpdatePosition(textBox, true); 
                    break;
                case Lizk.SimpleHUD.HUDCommands.PageUp:
                    break;
                case Lizk.SimpleHUD.HUDCommands.PageDown:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Select:
                    textBox.Label.Text += input.GetChar();
                    textBox.GetTextWidth();
                    input.UpdatePosition(textBox, true);
                    break;
                case Lizk.SimpleHUD.HUDCommands.Menu:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Web:
                    if (!string.IsNullOrEmpty(textBox.Label.Text.Trim()))
                    {
                        SharpMedia.ScraperHandler.Scrape(title, textBox.Label.Text);
                        SharpMedia.Back();
                    }
                    break;
                case Lizk.SimpleHUD.HUDCommands.Preset1:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Preset2:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Preset3:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Play:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Pause:
                    break;
                case Lizk.SimpleHUD.HUDCommands.PlayPause:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Stop:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Forward:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Rewind:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Next:
                    break;
                case Lizk.SimpleHUD.HUDCommands.Previous:
                    break;
                case Lizk.SimpleHUD.HUDCommands.KeyPress:
                    textBox.Label.Text += (char)tag;
                    textBox.GetTextWidth();
                    input.UpdatePosition(textBox, true);
                    input.SelectChar((char)tag);
                    break;
                default:
                    if (parent != null)
                        parent.Command(command, tag);
                    break;
            }
        }
    }
}
